package com.crm.services;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.druid.filter.AutoLoad;
import com.crm.model.jdao.IBackmoneyplanDao;
import com.crm.model.jdao.IOrderInfoDao;
import com.crm.model.jdao.IReturnMoneyDao;
import com.crm.model.mdao.DictionariesMapper;
import com.crm.model.mdao.ReturnMoneyMapper;
import com.crm.model.pojo.Backmoneyplan;
import com.crm.model.pojo.Customer;
import com.crm.model.pojo.Orderinfo;
import com.crm.model.pojo.Returnmoney;
import com.crm.model.pojo.Timeline;
import com.github.pagehelper.PageHelper;

/**
 * 订单业务类
 * @author 周远星
 *
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ReturnMoneyService {
	@Autowired
	ReturnMoneyMapper rmMapper;
	
	@Autowired
	DictionariesMapper dicMapper;
	
	@Autowired
	IReturnMoneyDao iRmdao;
	@Autowired
	IBackmoneyplanDao iBmpdao;
	@Autowired
	IOrderInfoDao iodao;
	@Autowired
	TimeLineServices timeLineServices;
	
	/**
	 * 分页查询查询回款记录在前台表格进行展示
	 * 
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public List<Map<String, String>> selectByPager(int pageNum, int pageSize,Integer statusid ,String search) {
		// 使用分页插件
		System.out.println("pageNum:" + pageNum + ",pageSize:" + pageSize);
		// 配置分页信息。这句话后面的第一个查询将调用分页。使用分页，对应的Service方法最好只写一个查询方法
		PageHelper.startPage(pageNum, pageSize);
		// 获取
		List<Map<String, String>> list = rmMapper.selectRmAll(search);
		System.out.println("大小" + list.size());
		return list;
	}
	
	/**
	 * 查询分类信息
	 * @return 
	 */
	public List<Map<String, String>> selectBySort() {
		return dicMapper.selectByDicpar(1273);
	}
	
	/**
	 * 查询付款信息
	 * @return 
	 */
	public List<Map<String, String>> selectByPay() {
		return dicMapper.selectByDicpar(1085);
	}
	
	/**
	 * 添加、修改回款记录
	 */
	public Returnmoney addBmp(Returnmoney returnmoney,Integer orderid,String id) {
		//改变回款状态
		if(!id.trim().equals("")||!id.trim().equals("0")) {
			Backmoneyplan backmoneyplan = iBmpdao.findById(Integer.parseInt(id)).get();
			backmoneyplan.setBmp_isBackMoney("已回");
			iBmpdao.save(backmoneyplan);
		}
		List<Map<String, String>> findByoId = rmMapper.findByoId(orderid);
		Orderinfo orderinfo = iodao.findById(orderid).get();
		//更新订单回款金额
		float redMax=0;
		for (Map<String, String> map : findByoId) {
			System.out.println(map);
			redMax+=Float.parseFloat(String.valueOf(map.get("money")));
		}
		
		System.out.println(new BigDecimal(redMax).add(returnmoney.getRmMoney()));
		orderinfo.setOreturned(new BigDecimal(redMax).add(returnmoney.getRmMoney()));
		iodao.save(orderinfo);
		Returnmoney rm = iRmdao.save(returnmoney);
		Timeline timeline=new Timeline();
		timeline.setTheme("回款");
		timeline.setTimes(new Date());
		timeline.setCustId(orderinfo.getCustomer().getCustId());
		timeline.setDetails("订单号:"+orderinfo.getOId()+"  		主题:"+orderinfo.getOTitle()+"订单			新增回款 ￥"+returnmoney.getRmMoney());
		timeLineServices.insertTimeLine(timeline);


		System.out.println(rm);
		return rm;
	}
	
	
	/**
	 * 根据id查询
	 */
	public List<Map<String, String>> findById(Integer id) {
		
		return rmMapper.findById(id);
	}
	
	/**
	 * 根据订单id查询回款记录
	 */
	public List<Map<String, String>> findByoId(Integer id) {
		
		return rmMapper.findByoId(id);
	}
	
	/**
	 * 逻辑删除回款计划，将状态改为1103
	 */
	public Returnmoney deleteRm(Integer id) {
		Returnmoney rm = iRmdao.findById(id).get();
		rm.setRmStatus(1103);
		Returnmoney save = iRmdao.save(rm);
		System.out.println(save);
		return save;
	}
	
	/**
	 * 根据客户id查询
	 * @param custid
	 * @return
	 */
	public List<Map<String, String>> findByCustid(Integer custid) {
		return rmMapper.findByCustid(custid);
	}
}
